﻿using System;
using System.Camera.Broadcast;
using System.Windows.Forms;

namespace ViewCamHost
{
    public partial class ClientForm : Form
    {
        #region Declarations
        /// <summary>
        /// The host
        /// </summary>
        CameraHost Host;

        /// <summary>
        /// The form
        /// </summary>
        SettingsForm form;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the folder path.
        /// </summary>
        /// <value>
        /// The folder path.
        /// </value>
        public string folderPath {get; set;}

        /// <summary>
        /// The ifile path
        /// </summary>
        private string ifilePath;
        #endregion

        #region Default Controls
        /// <summary>
        /// Initializes a new instance of the <see cref="ClientForm"/> class.
        /// </summary>
        public ClientForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the ClientForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ClientForm_Load(object sender, EventArgs e)
        {
            this.Text = "View Cam : Host";
            
            OnLoad();
            PlayerControls_PreLoad();
        }

        /// <summary>
        /// Handles the Click event of the btnClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            Host.CloseConnection();
            this.Close();
            Application.Exit();
        }

        /// <summary>
        /// Handles the FormClosing event of the ClientForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ClientForm_FormClosing(object sender, EventArgs e)
        {
            Host.CloseConnection();
            this.Dispose();
            Application.Exit();
        }

        /// <summary>
        /// Handles the Click event of the btnOpenFile control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            if(folderPath == null)
            {
                MessageBox.Show("Shared Folder Not Set.", "View Cam");
            }
            else
            {
                OpenFileDialog openFile = new OpenFileDialog();
                openFile.Filter = "AVI files (*.avi)|*.avi";
                openFile.InitialDirectory = folderPath.ToString();
                openFile.Title = "Select 1 Recorded Video.";

                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    string filePath;
                    filePath = openFile.FileName;
                    axWMP.URL = filePath;
                    PlayerControls_PostLoad();
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnPlay control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnPlay_Click(object sender, EventArgs e)
        {
            axWMP.Ctlcontrols.play();
        }

        /// <summary>
        /// Handles the Click event of the btnPause control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            axWMP.Ctlcontrols.pause();
        }

        /// <summary>
        /// Handles the Click event of the btnStop control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            axWMP.Ctlcontrols.stop();
        }
        #endregion

        #region Default Methods
        /// <summary>
        /// Called when [load].
        /// </summary>
        private void OnLoad()
        {
            Host = new CameraHost(picBox, 8080);
            Host.NoDelay = true;
            Host.StartConnection();

            Host.OnConnection += new CameraHost.OnConnectionEventHandler(Host_OnConnection);
            Host.LostConnection += new CameraHost.LostConnectionEventHandler(Host_LostConnection);
            Host.errEncounter += new CameraHost.errEncounterEventHandler(Host_ErrConnection);
        }

        /// <summary>
        /// Players the controls_ pre load.
        /// </summary>
        private void PlayerControls_PreLoad()
        {
            btnPlay.Enabled = false;
            btnPause.Enabled = false;
            btnStop.Enabled = false;
        }

        /// <summary>
        /// Players the controls_ post load.
        /// </summary>
        private void PlayerControls_PostLoad()
        {
            btnPlay.Enabled = true;
            btnPause.Enabled = true;
            btnStop.Enabled = true;
        }

        /// <summary>
        /// Host_s the lost connection.
        /// </summary>
        private void Host_LostConnection()
        {
            this.lblStatus.Text = "Disconnected";
        }

        /// <summary>
        /// Host_s the on connection.
        /// </summary>
        private void Host_OnConnection()
        {
            this.lblStatus.Text = "Connected";
        }

        /// <summary>
        /// Host_s the err connection.
        /// </summary>
        /// <param name="ex">The ex.</param>
        private void Host_ErrConnection(Exception ex)
        {
            this.lblStatus.Text = "Error";
        }

        /// <summary>
        /// Handles the Click event of the btnFolder control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnFolder_Click(object sender, EventArgs e)
        {
            form = new SettingsForm();

            form.clientForm = this;
            form.ShowDialog();
        }

        #endregion
    }
}
